package com.softwinner.bt;

import com.softwinner.bt.Fragment.CalllogFragment;
import com.softwinner.bt.Fragment.ContactFragment;
import com.softwinner.bt.Fragment.KeyBoardFragment;
import com.softwinner.bt.Fragment.MainFragment;
import com.softwinner.bt.Fragment.PairFragment;
import com.softwinner.bt.policy.BTManager;
import com.softwinner.bt.service.BtService;
import com.softwinner.bt.util.BTLog;
import com.softwinner.bt.util.GpioHelper;
import com.softwinner.bt.util.Utils;

import android.support.v4.app.FragmentActivity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends FragmentActivity{

	private ImageView mIcon;
	private TextView mLabel;
	private TextView mBtState;
	private TextView mBtName;

	MainFragment mMain;
	KeyBoardFragment mKeyBoard;
	ContactFragment mContact;
	PairFragment mPairList;
	CalllogFragment mCallLog;

	public static final int TYPE_MAIN = 0;
	public static final int TYPE_KEYBOARD = 1;
	public static final int TYPE_CONTACT = 2;
	public static final int TYPE_PAIRLIST = 3;
	public static final int TYPE_CALLLOG = 4;
	public int currentType = 0;

	private static int[] itemIconRes = { R.drawable.ic_bt,
			R.drawable.ic_keyboard_icon, R.drawable.ic_contact_icon,
			R.drawable.ic_pair_icon, R.drawable.ic_calllog_icon };
	private static int[] itemLabelRes = { R.string.app_name,
			R.string.title_keyboard, R.string.title_contact,
			R.string.title_pair, R.string.title_calllog };
	
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_main);
		mIcon = (ImageView) this.findViewById(R.id.main_iv_icon);
		mLabel = (TextView) this.findViewById(R.id.main_tv_label);
		mBtState = (TextView) this.findViewById(R.id.main_tv_state);
		mBtName = (TextView) this.findViewById(R.id.main_tv_name);		
		init();
		Intent BtService = new Intent(this, BtService.class);
		startService(BtService);
		
		IntentFilter filter = new IntentFilter();
		filter.addAction(Utils.ACTION_BTSTATE_CHANGED);
		filter.addAction(Utils.ACTION_GET_BTNAME);
		registerReceiver(receiver, filter);
	}
	
	private BroadcastReceiver receiver = new BroadcastReceiver() {		
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if(action.equals(Utils.ACTION_BTSTATE_CHANGED)){
				updateStatus();
			}else if(action.equals(Utils.ACTION_GET_BTNAME)){
				setBtName();
			}
		}
	};
	
	public void init() {
		initFragment();
		updateStatus();
		setBtName();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		if (currentType != 0) {
			SetFragment(0);
			return;
		}
		super.onBackPressed();
	}
	
	public void updateStatus() {
		if (GpioHelper.readBtStatus() == GpioHelper.BT_CLOSE) {
			updateStatus(BTManager.STATE_CLOSED);
			BtService.mBtStatus = -1;
		} else {
			if (BtService.mBtStatus != -1) {
				updateStatus(BtService.mBtStatus);
			} else {
				updateStatus(BTManager.STATE_UNCONNECTION);
			}
		}
	}

	public void updateStatus(int status) {
		switch (status) {
		case BTManager.STATE_CLOSED:
			mBtState.setText(R.string.states_closed);
			break;
		case BTManager.STATE_OPENING:
			mBtState.setText(R.string.states_opening);
			break;
		case BTManager.STATE_CONNECTING:
			mBtState.setText(R.string.states_connecting);
			break;
		case BTManager.STATE_CONNECTED:
			mBtState.setText(getResources().getString(R.string.states_connected) + BtService.mBtPairName);
			break;
		case BTManager.STATE_UNCONNECTION:
			mBtState.setText(R.string.states_unconnected);
			break;
		}
	}

	public void setBtName() {
		if (mBtName == null)
			mBtName = (TextView) this.findViewById(R.id.main_tv_name);
		BTLog.i(getClass(), "setBtName : " + BtService.mBtName);
		mBtName.setText(BtService.mBtName);
	}
	
	public int getState() {		
		return BtService.mBtStatus;
	}

	private void initFragment() {
		mMain = new MainFragment();
		mKeyBoard = new KeyBoardFragment();
		mContact = new ContactFragment();
		mPairList = new PairFragment();
		mCallLog = new CalllogFragment();
		SetFragment(TYPE_MAIN);
	}

	public void SetFragment(int type) {
		FragmentTransaction ft = this.getFragmentManager().beginTransaction();
		Fragment f = mMain;
		currentType = type;
		switch (type) {
		case TYPE_MAIN:
			f = mMain;
			break;
		case TYPE_KEYBOARD:
			f = mKeyBoard;
			break;
		case TYPE_CALLLOG:
			f = mCallLog;
			break;
		case TYPE_CONTACT:
			f = mContact;
			break;
		case TYPE_PAIRLIST:
			f = mPairList;
			break;
		}
		mIcon.setImageResource(itemIconRes[type]);
		mLabel.setText(this.getString(itemLabelRes[type]));
		ft.replace(R.id.bt_fragment_contains, f);
		ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		ft.commit();
	}
}
